<template>
  <component :is="type" v-bind="linkProps">
    <slot />
  </component>
</template>

<script setup>
import { computed } from 'vue'
import { isExternal } from '@/utils/validate'

const props = defineProps({
  to: {
    type: String,
    required: true
  }
})

// 根据链接类型决定使用a标签还是router-link
const type = computed(() => {
  return isExternal(props.to) ? 'a' : 'router-link'
})

// 根据链接类型生成对应的属性
const linkProps = computed(() => {
  if (isExternal(props.to)) {
    return {
      href: props.to,
      target: '_blank',
      rel: 'noopener'
    }
  } else {
    return {
      to: props.to
    }
  }
})
</script>
